import requests
from bs4 import BeautifulSoup
import os

url = "https://www.nfu.edu.cn/index/tzgg.htm"
resp = requests.get(url, timeout=10)
resp.encoding = resp.apparent_encoding
soup = BeautifulSoup(resp.text, "html.parser")

links = []
list_div = soup.find("div", class_="list", style="display: block;")
if list_div:
    for a in list_div.find_all("a", href=True):
        href = a['href']
        # 补全相对链接
        if not href.startswith("http"):
            href = os.path.join("https://www.nfu.edu.cn", href.lstrip("/"))
        links.append(href)

# 写入.env
with open(".env", "a", encoding="utf-8") as f:
    f.write(f"DEPT_LINKS={';'.join(links)}\n")

print("已写入院系链接到.env：")
for link in links:
    print(link) 